package net.hn.hnms.biz.cockpit.service.impl;


import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.cockpit.domain.vo.OptionVo;
import net.hn.hnms.biz.cockpit.domain.vo.ProductTrendVo;
import net.hn.hnms.biz.cockpit.domain.vo.SensorAlarmVo;
import net.hn.hnms.biz.cockpit.mapper.ProductCockpitMapper;
import net.hn.hnms.biz.cockpit.service.IProductCockpitService;
import net.hn.hnms.biz.cockpit.utils.OweDateUtils;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;


@Service
@RequiredArgsConstructor
public class ProductCockpitServiceImpl implements IProductCockpitService {

    private final ProductCockpitMapper productCockpitMapper;

    @Override
    public JSONObject getStaffTrend(String mineCode) {
        JSONObject result = new JSONObject();
        result.put("legendData", Collections.singletonList("井下人数"));
        result.put("unit", "人");
        String endDate = DateUtils.getTime();
        String beginDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.addHours(DateUtils.getNowDate(), -24));
        List<String> xData = OweDateUtils.getXData(beginDate, endDate, "hour");
        List<OptionVo> staffTrend = productCockpitMapper.getStaffTrend(beginDate, endDate, mineCode);
        xData.remove(xData.size() - 1);
        List<String> personList = Lists.newArrayList();
        xData.forEach(x->{
            OptionVo optionVo = staffTrend.stream().filter(item -> item.getName().equals(x)).findFirst().orElse(null);
            personList.add(optionVo == null ? "0" : optionVo.getValue());
        });
        result.put("xData", xData);
        result.put("yData", Collections.singletonList(personList));
        return result;
    }

    @Override
    public List<OptionVo> getMajorPlace(String mineCode) {
        return productCockpitMapper.getMajorPlace(mineCode);
    }

    @Override
    public JSONObject getMajorPlaceStaffTrend(String mineCode,String majorPlace) {
        JSONObject result = new JSONObject();
        result.put("legendData", Collections.singletonList(majorPlace));
        result.put("unit", "人");
        String endDate = DateUtils.getTime();
        String beginDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.addHours(DateUtils.getNowDate(), -24));
        List<String> xData = OweDateUtils.getXData(beginDate, endDate, "hour");
        List<OptionVo> staffTrend = productCockpitMapper.getMajorPlaceStaffTrend(beginDate, endDate, mineCode,majorPlace);
        xData.remove(xData.size() - 1);
        List<String> personList = Lists.newArrayList();
        xData.forEach(x->{
            OptionVo optionVo = staffTrend.stream().filter(item -> item.getName().equals(x)).findFirst().orElse(null);
            personList.add(optionVo == null ? "0" : optionVo.getValue());
        });
        result.put("xData", xData);
        result.put("yData", Collections.singletonList(personList));
        return result;
    }

    @Override
    public List<SensorAlarmVo> getSensorAlarm(String mineCode) {
        return productCockpitMapper.getSensorAlarm(mineCode);
    }

    @Override
    public List<OptionVo> getOverflowPoint(String mineCode) {
        return productCockpitMapper.getOverflowPoint(mineCode);
    }

    @Override
    public JSONObject getOverflowPointTrend(String mineCode, String overflowPoint) {
        JSONObject result = new JSONObject();
        result.put("legendData", Collections.singletonList(overflowPoint));
        result.put("unit", "立方米");
        String endDate = DateUtils.getTime();
        String beginDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.addHours(DateUtils.getNowDate(), -24));
        List<String> xData = OweDateUtils.getXData(beginDate, endDate, "hour");
        List<OptionVo> staffTrend = productCockpitMapper.getOverflowPointTrend(beginDate, endDate, mineCode,overflowPoint);
        xData.remove(xData.size() - 1);
        List<String> personList = Lists.newArrayList();
        xData.forEach(x->{
            OptionVo optionVo = staffTrend.stream().filter(item -> item.getName().equals(x)).findFirst().orElse(null);
            personList.add(optionVo == null ? "0" : optionVo.getValue());
        });
        result.put("xData", xData);
        result.put("yData", Collections.singletonList(personList));
        return result;
    }


}
